<?php defined('SYSPATH') or die('No direct script access.');

class Controller_BoBunddleItem extends Controller_Layout {
	public $auth_required = TRUE;

	public function action_index()
	{
		$bunddleItem = ORM::factory("bunddleItem");
		$result_bunddleItem = $bunddleItem->where("status","=","A")->find_all();
		
	}
	
	public function action_insertPage(){
		$view = View::factory("pages/bo/dish_bunddle_item_add_page");
		$view->bunddleid = $_GET["bunddleid"];
		exit($view->render());
	}

	public function action_insert(){
		$bunddleItem = ORM::factory("bunddleItem");
		$fileUtil = FileUtil::Factory();
		$fileName = $fileUtil->saveFile(Upload::$default_directory.DIRECTORY_SEPARATOR.'bunddleItem');
		if ($fileName != FALSE){
			$fileUtil->setFilePath($fileName,'picture');
		}
		$_POST["status"] = "A";
		if($bunddleItem->values($_POST)->check()){
			ORM::factory("supplier",$_POST["supplierid"])->updateModifyTime();
			$bunddleItem->save();
			$this->baseInfo($_POST["bunddleid"]);
		}
	}

	public function action_updatePage(){
		$bunddleItem = ORM::factory("bunddleItem",$_GET["bunddleItemId"]);
		$view = View::factory("pages/bo/dish_bunddle_item_edit_page");
		$view->result = $bunddleItem;
		exit($view->render());
	}
	
	public function action_update(){
		$bunddleItem = ORM::factory("bunddleItem",$_POST["id"]);
		$bunddleItem->name = $_POST["name"];
		$bunddleItem->originalprice = $_POST["originalprice"];
		$bunddleItem->discountprice = $_POST["discountprice"];
		$bunddleItem->quantity = $_POST["quantity"];
		$bunddleItem->desc = $_POST["desc"];
		
		$fileUtil = FileUtil::Factory();
		$fileUtil->setFilePath($fileUtil->saveFile(Upload::$default_directory.DIRECTORY_SEPARATOR.'bunddleItem'),'picture');
		if (!empty($_POST['picture'])){
			if ($_POST['picture']!=$bunddleItem->picture){
				if (!empty($bunddleItem->picture)){
					$fileUtil->deleteFile($bunddleItem->picture);
				}
				$bunddleItem->picture = $_POST['picture'];
			}
		}		
		
		if($bunddleItem->save()){
			ORM::factory("supplier",$bunddleItem->supplierid)->updateModifyTime();
			$this->baseInfo($_POST["bunddleid"]);
		}
		
	}
	
	private function baseInfo($bunddleid){
		$bunddleItem = ORM::factory("bunddleItem");
		$result_bunddleItem = $bunddleItem->where("bunddleid","=",$bunddleid)->and_where("status","=","A")->find_all();
		$view = View::factory("pages/bo/dish_bunddle_item_page");
		$view->result_bunddleItem = $result_bunddleItem;
		$bunddle = ORM::factory("bunddle",$bunddleid);
		$view->result_supplier_name = ORM::factory("supplier",$bunddle->supplierid)->name;
		$view->result_bunddle_name = $bunddle->name;
		exit($view->render());
	}
	
	public function action_show(){
		$this->baseInfo($_GET["bunddleid"]);
	}
	
	public function action_deleteAll(){
		$bunddleItem = ORM::factory("bunddleItem");
		$bunddleItem->status = "I";
		$ids = $_POST["ids"];
		foreach (split(",", $_POST["ids"]) as $id){
			if (!empty($id)){
				$bunddleItem->or_where("id","=",$id);
				if (empty($supplierId)){
					$supplierId = ORM::factory("bunddleItem",$id)->supplierid;
				}
			}
		}
		if($bunddleItem->save_all()){
			ORM::factory("supplier",$supplierId)->updateModifyTime();
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除菜色成功";
			exit($view->render());
		}
	}
}
